package com.common.base.app.fragment

import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.common.base.R
import com.common.base.app.activity.BaseVMActivity
import com.common.base.app.extras.no
import com.common.base.app.extras.showToast
import com.common.base.app.extras.yes
import com.common.base.app.viewmodel.*
import com.common.base.utils.LogUtils
import com.common.base.widget.LoadingDialog
import java.lang.ref.WeakReference


/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/19
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
abstract class BaseVMFragment<VB : ViewBinding, VM : BaseViewModel> : BaseVBFragment<VB>() {
    var loadingDialog: WeakReference<LoadingDialog>? = null
    lateinit var mViewModel: VM

    override fun initViewPre() {
        super.initViewPre()
        mViewModel = ViewModelProvider(this, ViewModelFactory()).get(getViewModel())
        initViewModelAction()
    }


    abstract fun getViewModel(): Class<VM>


    private fun initViewModelAction() {
        mViewModel.mStateLiveData.observe(this) { stateActionState ->
            when (stateActionState) {
                SuccessState -> {
                    loadFinish()
                    if (this !is BaseRecyclerVMFragment<*, *, *>) {
                        getPlaceholderView()?.isLoading()?.yes { showContent() }
                    }
                }

                is ErrorState -> {
                    LogUtils.e(BaseVMActivity.TAG, "errorState: ${stateActionState.message}")
                    loadFinish()
                    showError()
                    showErrorToast(stateActionState.message)
//                    stateActionState.message?.apply { showToast(this) }
                }

                is ErrorStateAnMsg -> {
                    LogUtils.e(BaseVMActivity.TAG, "errorState: ${stateActionState.message}")
                    loadFinish()
                    showError()
                    showErrorToast("errorState: ${stateActionState.state}------> " + "errorMsg: ${stateActionState.message}")
                }

                else -> {}
            }
        }
        mViewModel.mFailureLiveData.observe(this) {
            requireActivity().isFinishing.no { loadingDialog?.get()?.dismiss() }
            loadFinish()
        }
    }

    fun showDialogLoading(mCancelable: Boolean = true,content: String = resources.getString(R.string.loading_msg)) {
        if (loadingDialog == null || loadingDialog?.get() == null) {
            loadingDialog = WeakReference(LoadingDialog.newInstance(content,mCancelable))
        }

        if (loadingDialog?.get()?.isShowing() == true) {
            loadingDialog?.get()?.dismiss()
        }
        loadingDialog?.get()?.show(childFragmentManager)
    }

    fun dismissLoadingDialog() {
        if (requireActivity().isFinishing.not()) {
            loadingDialog?.get()?.dismiss()
        }
    }

    /**
     * 数据加载完成 成功/失败
     */
    open fun loadFinish() {
        dismissLoadingDialog()
    }


    /**
     * 默认错误提示
     */
    open fun showErrorMsg() {
//        Toast.makeText(this, "数据异常，请关闭页面后重新打开尝试！", Toast.LENGTH_SHORT).show();
        showToast(R.string.common_error_tip)
    }
}